﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace BAL.EstRec.FDVIndicador 
{
    public class CapFDVIndicador : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        #region fields
        Pos pos;
        int _tiempoImplementacion;
        int _visitasSugeridas;
        string _iniciativas;
        int _numIniciativas;
        int _numRecursos;
        FDVRecursoRule rule;
        bool _isRuleExceded;
        bool _isMaxIniciativaRuleExceded;

        bool _isSomeMaxRuleExceded;
        #endregion

        #region  properties
        public Pos Pos
        {
            get { return pos; }
            set {
                if (value != pos)
                {
                    pos = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("Pos"));
                    }
                }
            }
        }
        
        public int TiempoImplementacion
        {
            get { return _tiempoImplementacion; }
            set { 
                if (value != _tiempoImplementacion)
                {
                    _tiempoImplementacion = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("TiempoImplementacion"));
                    }
                }
            }
        }

        public int VisitasSugeridas
        {
            get { return _visitasSugeridas; }
            set { 
                if (value != _visitasSugeridas)
                {
                    _visitasSugeridas = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("VisitasSugeridas"));
                    }
                }
            }
        }

        public string Iniciativas
        {
            get { return _iniciativas; }
            set { 
                if (value != _iniciativas)
                {
                    _iniciativas = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("Iniciativas"));
                    }
                }
            }
        }

        public int NumIniciativas
        {
            get { return _numIniciativas; }
            set {                
               if (value != _numIniciativas)
               {
                   _numIniciativas = value;
                   this.calculateIsRuleExceded();
                   if (this.PropertyChanged != null)
                   {
                       this.PropertyChanged(this, new PropertyChangedEventArgs("NumIniciativas"));
                   }
               }
            }
        }

        public int NumRecursos { 
            get{ return _numRecursos;}
            set
            {
               if (value != _numRecursos)
               {
                   _numRecursos = value;
                   this.calculateIsRuleExceded();
                   if (this.PropertyChanged != null)
                   {
                       this.PropertyChanged(this, new PropertyChangedEventArgs("NumRecursos"));
                   }
               }
            } 
        }

        public bool IsRuleExceded
        {
            get { return _isRuleExceded; }
            set
            {
                if (value != _isRuleExceded)
                {
                    _isRuleExceded = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("IsRuleExceded"));
                    }
                }
            }
        }

        public bool IsMaxIniciativaRuleExceded
        {
            get { return _isMaxIniciativaRuleExceded; }
            set
            {
                if (value != _isMaxIniciativaRuleExceded)
                {
                    _isMaxIniciativaRuleExceded = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("IsMaxIniciativaRuleExceded"));
                    }
                }
            }
        }


        public bool IsSomeMaxRuleExceded
        {
            get { return _isSomeMaxRuleExceded; }
            set
            {
                if (value != _isSomeMaxRuleExceded)
                {
                    _isSomeMaxRuleExceded = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("IsSomeMaxRuleExceded"));
                    }
                }
            }
        }
        #endregion


        public CapFDVIndicador()
        { }

        public CapFDVIndicador(Pos p)
        {
            this.pos = p;            
        }

        //public CapFDVIndicador(Pos p, int tiempoImplement, int visitasSug, int numIniciativas)
        //{
        //    this.Pos = p;
        //    this.TiempoImplementacion = tiempoImplement;
        //    this.VisitasSugeridas = visitasSug;
        //    this.NumIniciativas = numIniciativas;
        //}

        //public CapFDVIndicador(Pos p, int tiempoImplement, int visitasSug, int numIniciativas,int numRecursos)
        //{
        //    this.Pos = p;
        //    this.TiempoImplementacion = tiempoImplement;
        //    this.VisitasSugeridas = visitasSug;
        //    this.NumIniciativas = numIniciativas;
        //    this.NumRecursos = numRecursos;
        //}

        public CapFDVIndicador(Pos p, int tiempoImplement, int visitasSug, int numIniciativas, int numRecursos,FDVRecursoRule r)
        {
            this.Pos = p;
            this._tiempoImplementacion = tiempoImplement;
            this._visitasSugeridas = visitasSug;
            this._numIniciativas = numIniciativas;
            this._numRecursos = numRecursos;
            this.rule = r;
            this._isMaxIniciativaRuleExceded = false;
            this._isRuleExceded = false;
            this.calculateIsRuleExceded();
        }

        private void calculateIsRuleExceded()
        {
            if (this._numRecursos > this.rule.MaxRecursoPorPos || this._numIniciativas > this.rule.MaxInicitivaPorPos)
            {
                this.IsRuleExceded = true;
            }
            else
            {
                this.IsRuleExceded = false;
            }
        }

        public void Calculate(string idPos, CapturasPFDV oCPFDV)
        {
            // iniciativas
            int cnt = 0;
            string inic = "";

            foreach (CapturaPivotFDV item in oCPFDV.ObCollPFDV)
            {
                if(item.recurso.excReglaIniciativa)
                    cnt++;
            }

            if (cnt > 2)
                inic = "Excedidas";
            else inic = "OK";

            // Visitas sugeridas
            var v = from o in oCPFDV.ObCollPFDV
                    where o.pos.idPos == idPos
                    group o by o.recurso into g
                    select new { Recurso = g.Key, maxVal = g.Max(c => c.cantidad) };

            var y = (
                    from o in oCPFDV.ObCollPFDV
                    where o.pos.idPos == idPos
                    group o by o.pos into g
                    select new { Pos = g.Key, maxVal = g.Max(c => c.recurso.visitasSugeridas) }
                    ).Max(m  => m.maxVal);
             
            // tiempo implementación
            var x = (from o in oCPFDV.ObCollPFDV
                    where o.pos.idPos == idPos
                    group o by o.pos into g
                    select new { Pos = g.Key, sumVal = g.Sum(s => s.recurso.tiempoImplementacion * s.cantidad) }
                    ).Sum(s => s.sumVal);

            
        }//
    }
}
